package com.xinlu.common.game.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.xinlu.common.game.bo.BankBackBo;
import com.xinlu.common.game.bo.ConstantLstBo;
import com.xinlu.common.game.form.TdbbForm;
import com.xinlu.derek.common.BaseAction;
import com.xinlu.derek.common.BaseForm;
import com.xinlu.persistence.Users;
import com.xinlu.user.bo.UsersBo;
import com.xinlu.util.tool.DateUtil;

public class TdbbAction extends BaseAction {

	private BankBackBo bankBackBo;
	private UsersBo usersBo;
	TdbbForm fr = new TdbbForm();

	public void setBankBackBo(BankBackBo bankBackBo) {
		this.bankBackBo = bankBackBo;
	}

	public void setFr(TdbbForm fr) {
		this.fr = fr;
	}

	@Override
	public String doExecute() throws Exception {
		return null;
	}

	@Override
	public void formInit() {
	}

	@Override
	public BaseForm getModel() {
		return fr;
	}

	/**
	 * 加载页面
	 * 
	 * @return
	 */
	public String loadPage() {

		fr.setCaiZhongs(ConstantLstBo.getCaiZhongs());
		fr.setCaiZhong("--全部--");
		Date date = new Date();
		
		if (ConstantLstBo.isNullOrEmpty(fr.getBgnDate())) {
			String bgnDate = DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, date);
			String endDate = DateUtil.getFormatDate(DateUtil.YYYY_MM_DD,
					DateUtils.add(date, 5, 1));
			fr.setBgnDate(bgnDate + ConstantLstBo.getBgnHMS(bgnDate));
			fr.setEndDate(endDate + ConstantLstBo.getBgnHMS(endDate));
		}
		
		Users yh = null;
		if(ConstantLstBo.isNullOrEmpty(fr.getCrtYhm())){
			yh = (Users) getSession().getAttribute("userinfo");
		}else{
			yh = usersBo.loadUser(fr.getCrtYhm());
		}
		
		fr.setTdbbs(bankBackBo.loadTdbbs(yh, fr.getCaiZhong(), fr.getBgnDate(), fr.getEndDate()));
		fr.setDqyhbb(bankBackBo.loadYhbb(yh, fr.getCaiZhong(), fr.getBgnDate(), fr.getEndDate()));
		List lst = fr.getTdbbs();
		List lst2 = new ArrayList();
		for (int i = 0; i < lst.size(); i++) {
			lst2.add(lst.get(i));
		}
		lst2.add(fr.getDqyhbb());
		fr.setBbzj(bankBackBo.loadBbzj(lst2));

		return "tdbb";
	}

	/**
	 * 加载团队报表
	 * 
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public String loadTdbb() throws UnsupportedEncodingException {

		fr.setCaiZhongs(ConstantLstBo.getCaiZhongs());

		Users yh = null;
		if(ConstantLstBo.isNullOrEmpty(fr.getCrtYhm())){
			yh = (Users) getSession().getAttribute("userinfo");
		}else{
			yh = usersBo.loadUser(fr.getCrtYhm());
		}
		
		fr.setTdbbs(bankBackBo.loadTdbbs(yh, "--全部--", fr.getBgnDate(), fr.getEndDate()));
		fr.setDqyhbb(bankBackBo.loadYhbb(yh, "--全部--", fr.getBgnDate(), fr.getEndDate()));
		List lst = fr.getTdbbs();
		List lst2= new ArrayList();
		for(int i=0;i<lst.size();i++){
			lst2.add(lst.get(i));
		}
		lst2.add(fr.getDqyhbb());
		fr.setBbzj(bankBackBo.loadBbzj(lst2));

		return "tdbb";
	}

	public void setUsersBo(UsersBo usersBo) {
		this.usersBo = usersBo;
	}

}
